package scau.erps.view.system.teacher;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import scau.erps.app.Erps;
import scau.erps.entity.OrderSet;
import scau.erps.service.OrderSetService;
import scau.erps.service.impl.OrderSetServiceImpl;
import scau.erps.util.OrderGenerator;

/** 
 * 订单参数设置面板
 * @author  outsider
 * 
 */
public class OrderSetPanel extends JPanel implements ActionListener{
	private static final long serialVersionUID = 1L;
	private JPanel contentPanel,actionPanel;
	private JComboBox<String> yearCombo,productCombo,marketCombo;
	private JButton filterBtn,editBtn,refreshBtn,createBtn;
	private JTable table;
	private JScrollPane dataPanel;
	private DefaultTableCellRenderer render;
	private OrderSetListModel dataModel;
	public OrderSetPanel(){
		yearCombo = new JComboBox<String>(new String[]{"选择年份","Y1","Y2","Y3","Y4","Y5","Y6","Y7","Y8"});
		productCombo = new JComboBox<String>(new String[]{"选择产品","Beryl","Crystal","Ruby","Sapphire"});
		marketCombo = new JComboBox<String>(new String[]{"选择市场","本地","区域","国内","亚洲","国际"});		
		filterBtn = new JButton("筛选");		
		editBtn = new JButton("编辑");
		refreshBtn = new JButton("刷新");			
		createBtn = new JButton("一键生成订单");
		filterBtn.addActionListener(this);
		editBtn.addActionListener(this);
		refreshBtn.addActionListener(this);
		createBtn.addActionListener(this);
		actionPanel = new JPanel(); //顶部交互操作面板
		actionPanel.add(yearCombo);
		actionPanel.add(productCombo);
		actionPanel.add(marketCombo);
		actionPanel.add(filterBtn);			
		actionPanel.add(refreshBtn);
		actionPanel.add(editBtn);
		actionPanel.add(createBtn);
		table = new JTable();
		table.setPreferredScrollableViewportSize(new Dimension(900, 450));
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		dataPanel = new JScrollPane(table);
		dataPanel.setViewportView(table);
		render = new DefaultTableCellRenderer();   
		render.setHorizontalAlignment(JLabel.CENTER);   
		table.setDefaultRenderer(Object.class,render);
		contentPanel = new JPanel();
  		contentPanel.setLayout(new BorderLayout());
		contentPanel.add(actionPanel,BorderLayout.NORTH);
		contentPanel.add(dataPanel,BorderLayout.CENTER);
		this.add(contentPanel);
		this.setVisible(true);
		this.refresh();		
	}
	/**
	 * 刷新
	 */
	private void refresh(){
		this.dataModel = getOrderSetModel();
		this.table.setModel(dataModel);
		TableColumn column = table.getColumnModel().getColumn(0); //隐藏第一列
		column.setMinWidth(0);   
		column.setMaxWidth(0);
	}
	/**
	 * 加载数据模型
	 * @return
	 */
	private OrderSetListModel getOrderSetModel(){
		OrderSetService orderSetService = Erps.getIocInstance().get(OrderSetServiceImpl.class);	
		List<OrderSet> list = orderSetService.getAllOrderSet();
		dataModel = new OrderSetListModel(list);		
		return dataModel;
				
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==filterBtn){
			final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(dataModel);
			List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
			String year = null;
			if(yearCombo.getSelectedIndex() != 0 ){
				year = (String) yearCombo.getSelectedItem();
			}
			String product = null;
			if(productCombo.getSelectedIndex() != 0 ){
				product = (String) productCombo.getSelectedItem();
			}
			String market = null;
			if(marketCombo.getSelectedIndex() != 0 ){
				market = (String) marketCombo.getSelectedItem();
			}
			if(year !=null){
				filters.add(RowFilter.regexFilter(year));
			}
			if(product !=null){
				filters.add(RowFilter.regexFilter(product));
			}
			if(market !=null){
				filters.add(RowFilter.regexFilter(market));
			}
			sorter.setRowFilter(RowFilter.andFilter(filters));
			this.table.setRowSorter(sorter); 
            this.table.setModel(dataModel);
		}
		else if(e.getSource()==editBtn){
			int row = this.table.getSelectedRow();
			if (row == -1) {
				JOptionPane.showConfirmDialog(this, "未选中任何行", "提示",
						JOptionPane.CLOSED_OPTION);
			} else {
				int id=(int) this.table.getValueAt(row, 0);
				OrderSetService orderSetService=Erps.getIocInstance().get(OrderSetServiceImpl.class);
				OrderSet orderSet=orderSetService.getOrderSetById(id);
				new OrderSetEditDialog(orderSet);
				this.refresh();
				
			}
		}
		else if(e.getSource()==refreshBtn){
			this.refresh();
		}
		else if(e.getSource()==createBtn){
			OrderSetService orderSetService=Erps.getIocInstance().get(OrderSetServiceImpl.class);
			List<OrderSet> list=orderSetService.getAllOrderSet();
			int teamNum,year;
			teamNum=Integer.parseInt(JOptionPane.showInputDialog(this,"请输入小组数目"));
			year=Integer.parseInt(JOptionPane.showInputDialog(this,"请输入游戏的进行年限（当前仅支持生成Y1~Y7年的订单）"));
			int tot=OrderGenerator.preGenerateOrder(list, teamNum, year);
			String message = "生成订单"+tot+ "张";
			JOptionPane.showMessageDialog(null, message);			
		}
	}
	
}	

/**
 * 竞价管理（匹配）数据模型——教师
 * @author DCJ
 *
 */
class OrderSetListModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;	
		private String[] title = {"序号","年份","产品","市场","产品总量","参考价"};
		private Object[][] data= null;
		public OrderSetListModel(List<OrderSet> list){
			data =  new Object[list.size()][title.length];
			for (int i = 0; i < list.size(); i++) {
				data[i][0] = list.get(i).getId(); 
				data[i][1] = list.get(i).getYear();
				data[i][2] = list.get(i).getProduct();
				data[i][3] = list.get(i).getMarket();
				data[i][4] = list.get(i).getTotal();
				data[i][5] = list.get(i).getPrice();
			}	
	}

	@Override
	public int getRowCount() {
		return this.data.length; //行数
	}

	@Override
	public int getColumnCount() {
		return this.title.length; //列数
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return  data[rowIndex][columnIndex];  //取表格单元格值
	}
	public String getColumnName(int columnIndex) {
        return title[columnIndex];  //显示标题
    }

}
